﻿using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
/// <summary>
/// 枚举反射扩展
/// </summary>
public static class ESF_EnumReflectionExtend
{
    #region FirstAttribute 获得指定枚举的属性特性
    /// <summary>
    /// 获得指定枚举的属性特性
    /// </summary>
    /// <typeparam name="T">特性类型</typeparam>
    /// <param name="_value">枚举值</param>
    /// <returns>属性特性</returns>
    public static T ESF_FirstAttribute<T>(this Enum _value)
        where T : Attribute
    {
        Type type = _value.GetType();
        Dictionary<string, T> dic = type.ESF_NameToAttribute<T>();
        T attr = default(T);
        string key = _value.ToString();
        dic.TryGetValue(key, out attr);
        return attr;
    }
    #endregion

    #region EnumToX 枚举到指定类型

    #region EnumToName
    /// <summary>
    /// 枚举到名称
    /// Key:Enum
    /// Value:Name
    /// </summary>
    /// <typeparam name="E">枚举类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举到名称</returns>
    public static Dictionary<E, string> ESF_EnumToName<E>(this Type _enumType)
        where E : Enum
    {
        OnIsEnum<E>();
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (E)eEnum;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eEnum.ToString();
            }
        );
    }
    #endregion

    #region EnumToValue
    /// <summary>
    /// 枚举到值
    /// Key:Enum
    /// Value:Name
    /// </summary>
    /// <typeparam name="E">枚举类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举到名称</returns>
    public static Dictionary<E, int> ESF_EnumToValue<E>(this Type _enumType)
        where E : Enum
    {
        OnIsEnum<E>();
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (E)eEnum;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return (int)eValue;
            }
        );
    }

    /// <summary>
    /// 枚举到值
    /// Key:Enum
    /// Value:Name
    /// </summary>
    /// <typeparam name="E">枚举类型</typeparam>
    /// <typeparam name="V">值类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举到名称</returns>
    public static Dictionary<E, V> ESF_EnumToValue<E, V>(this Type _enumType)
        where E : Enum
    {
        OnIsEnum<E>();
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (E)eEnum;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return (V)eValue;
            }
        );
    }

    /// <summary>
    /// 枚举到值
    /// Key:Enum
    /// Value:Name
    /// </summary>
    /// <typeparam name="E">枚举类型</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <typeparam name="V">Value类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcResult">Value函数</param>
    /// <returns>枚举到名称</returns>
    public static Dictionary<E, V> ESF_EnumToValue<E, A, V>(this Type _enumType, Func<object, object,MemberInfo, A, V> _funcResult)
        where A : Attribute
        where E : Enum
    {
        OnIsEnum<E>();
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (E)eEnum;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return _funcResult.Invoke(eEnum, eValue, eFieldInfo, _enumForAttribute<A>(eFieldInfo)[0]);
            }
        );
    }
    #endregion

    #region EnumToAttribute
    /// <summary>
    /// 枚举到Attribute
    /// Key:Enum
    /// Value:Name
    /// </summary>
    /// <typeparam name="E">枚举类型</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举到名称</returns>
    public static Dictionary<E, A> ESF_EnumToAttribute<E, A>(this Type _enumType)
        where A : Attribute
        where E : Enum
    {
        OnIsEnum<E>();
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (E)eEnum;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return _enumForAttribute<A>(eFieldInfo)[0];
            }
        );
    }
    #endregion

    #region EnumToSpecialValue
    /// <summary>
    /// 枚举到特殊值
    /// </summary>
    /// <typeparam name="E">枚举</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcResult">Result函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<E, R> ESF_EnumToSpecialValue<E, A, R>(this Type _enumType, Func<E, A, R> _funcResult)
        where A : Attribute
        where E : Enum
    {
        return _enumType.ESF_EnumToSpecialValue<E, A, R>((ee, ev, ef, a) => { return _funcResult.Invoke((E)ee, a); });
    }

    /// <summary>
    /// 枚举值到特殊值
    /// </summary>
    /// <typeparam name="E">枚举</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcResult">Result函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<E, R> ESF_EnumToSpecialValue<E, A, R>(this Type _enumType, Func<object, object, MemberInfo, A,R> _funcResult)
        where A : Attribute
        where E : Enum
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (E)eEnum;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return _funcResult.Invoke(eEnum, eValue, eFieldInfo, _enumForAttribute<A>(eFieldInfo)[0]);
            }
        );
    }
    #endregion

    #region SpecialValueToEnum
    /// <summary>
    /// 特殊值到枚举
    /// </summary>
    /// <typeparam name="E">枚举</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcResult">Result函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<R, E> ESF_SpecialValueToEnum<R, A, E>(this Type _enumType, Func<E, A, R> _funcResult)
        where A : Attribute
        where E : Enum
    {
        return _enumType.ESF_SpecialValueToEnum<R, A, E>((ee, ev, ef, a) => { return _funcResult.Invoke((E)ee, a); });
    }

    /// <summary>
    /// 枚举值到特殊值
    /// </summary>
    /// <typeparam name="E">枚举</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcResult">Result函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<R, E> ESF_SpecialValueToEnum<R, A, E>(this Type _enumType, Func<object, object, MemberInfo, A, R> _funcResult)
        where A : Attribute
        where E : Enum
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return _funcResult.Invoke(eEnum, eValue, eFieldInfo, _enumForAttribute<A>(eFieldInfo)[0]);
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return (E)eEnum;
            }
        );
    }
    #endregion
    #endregion

    #region NameToX 枚举名称到指定类型

    #region NameToEnum
    /// <summary>
    /// 枚举名称与值
    /// </summary>
    /// <typeparam name="E">枚举类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<string, E> ESF_NameToEnum<E>(this Type _enumType)
        where E : Enum
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return eEnum.ToString();
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return (E)eValue;
            }
        );
    }
    #endregion

    #region NameToValue
    /// <summary>
    /// 枚举名称与值
    /// </summary>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<string, int> ESF_NameToValue(this Type _enumType)
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return eEnum.ToString();
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return (int)eValue;
            }
        );
    }

    /// <summary>
    /// 枚举名称与值
    /// </summary>
    /// <typeparam name="V">值类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<string, V> ESF_NameToValue<V>(this Type _enumType)
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return eEnum.ToString();
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return (V)eValue;
            }
        );
    }

    /// <summary>
    /// 枚举名称与值
    /// </summary>
    /// <typeparam name="A">属性类型</typeparam>
    /// <typeparam name="V">值类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcResult">Value函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<string, V> ESF_NameToValue<A, V>(this Type _enumType, Func<object, object, MemberInfo, A, V> _funcResult)
        where A : Attribute
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return eEnum.ToString();
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return _funcResult.Invoke(eEnum, eValue, eFieldInfo, _enumForAttribute<A>(eFieldInfo)[0]);
            }
        );
    }
    #endregion

    #region NameToAttribute
    /// <summary>
    /// 枚举名称与值
    /// </summary>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<string, A> ESF_NameToAttribute<A>(this Type _enumType)
        where A : Attribute
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return eEnum.ToString();
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return _enumForAttribute<A>(eFieldInfo)[0];
            }
        );
    }
    #endregion

    #endregion

    #region ValueToX 枚举值到枚举

    #region ValueToEnum
    /// <summary>
    /// 枚举值到枚举
    /// </summary>
    /// <typeparam name="E">枚举类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<int, E> ESF_ValueToEnum<E>(this Type _enumType)
        where E : Enum
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (int)eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return (E)eEnum;
            }
        );
    }

    /// <summary>
    /// 枚举值到枚举
    /// </summary>
    /// <typeparam name="E">枚举类型</typeparam>
    /// <typeparam name="V">值类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<V, E> ESF_ValueToEnum<V, E>(this Type _enumType)
        where E : Enum
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (V)eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return (E)eEnum;
            }
        );
    }

    /// <summary>
    /// 枚举名称与值
    /// </summary>
    /// <typeparam name="A">属性类型</typeparam>
    /// <typeparam name="V">值类型</typeparam>
    /// <typeparam name="E">枚举类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcKey">Key函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<V, E> ESF_ValueToEnum<V, A, E>(this Type _enumType, Func<object, object, MemberInfo,A,V> _funcKey)
        where A : Attribute
        where E : Enum
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return _funcKey.Invoke(eEnum, eValue, eFieldInfo, _enumForAttribute<A>(eFieldInfo)[0]);
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return (E)eEnum;
            }
        );
    }
    #endregion

    #region ValueToName
    /// <summary>
    /// 枚举值到名称
    /// </summary>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<int, string> ESF_ValueToName(this Type _enumType)
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (int)eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eEnum.ToString();
            }
        );
    }

    /// <summary>
    /// 枚举值到名称
    /// </summary>
    /// <typeparam name="V">值类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<V, string> ESF_ValueToName<V>(this Type _enumType)
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (V)eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eEnum.ToString();
            }
        );
    }

    /// <summary>
    /// 枚举值到名称
    /// </summary>
    /// <typeparam name="V">值类型</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcResult">Result函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<V, string> ESF_ValueToName<V, A>(this Type _enumType, Func<object, object, MemberInfo, A, string> _funcResult)
        where A : Attribute
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (V)eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return _funcResult.Invoke(eEnum, eValue, eFieldInfo, _enumForAttribute<A>(eFieldInfo)[0]);
            }
        );
    }
    #endregion

    #region ValueToSpecialValue
    /// <summary>
    /// 枚举值到特殊值
    /// </summary>
    /// <typeparam name="V">值类型</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcResult">Result函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<V, R> ESF_ValueToSpecialValue<V, A, R>(this Type _enumType, Func<V, A, R> _funcResult)
        where A : Attribute
    {
        return _enumType.ESF_ValueToSpecialValue<V, A, R>((ee, ev, ef, a) => { return _funcResult.Invoke((V)ev, a); });
    }

    /// <summary>
    /// 枚举值到特殊值
    /// </summary>
    /// <typeparam name="V">值类型</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcResult">Result函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<V, R> ESF_ValueToSpecialValue<V, A, R>(this Type _enumType, Func<object, object, MemberInfo, A, R> _funcResult)
    where A : Attribute
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (V)eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return _funcResult.Invoke(eEnum, eValue, eFieldInfo, _enumForAttribute<A>(eFieldInfo)[0]);
            }
        );
    }
    #endregion

    #region SpecialValueToValue
    /// <summary>
    /// 枚举值到特殊值
    /// </summary>
    /// <typeparam name="V">值类型</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcResult">Result函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<R, V> ESF_SpecialValueToValue<R, A, V>(this Type _enumType, Func<V, A, R> _funcResult)
        where A : Attribute
    {
        return _enumType.ESF_SpecialValueToValue<R, A, V>((ee, ev, ef, a) => { return _funcResult.Invoke((V)ev, a); });
    }

    /// <summary>
    /// 枚举值到特殊值
    /// </summary>
    /// <typeparam name="V">值类型</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcResult">Result函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<R, V> ESF_SpecialValueToValue<R, A, V>(this Type _enumType, Func<object, object, MemberInfo, A, R> _funcResult)
    where A : Attribute
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return _funcResult.Invoke(eEnum, eValue, eFieldInfo,_enumForAttribute<A>(eFieldInfo)[0]);
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return (V)eValue;
            }
        );
    }
    #endregion

    #region ValueToAttribute
    /// <summary>
    /// 枚举名称与值
    /// </summary>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<int, A> ESF_ValueToAttribute<A>(this Type _enumType)
        where A : Attribute
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (int)eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return _enumForAttribute<A>(eFieldInfo)[0];
            }
        );
    }

    /// <summary>
    /// 枚举名称与值
    /// </summary>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<V, A> ESF_ValueToAttribute<V, A>(this Type _enumType)
        where A : Attribute
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return (V)eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return _enumForAttribute<A>(eFieldInfo)[0];
            }
        );
    }
    #endregion

    #region SpecialValueToSpecialValue
    /// <summary>
    /// 特殊值到特殊值
    /// </summary>
    /// <typeparam name="K">Key类型</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <typeparam name="V">Value类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcResult">Result函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<K, V> ESF_SpecialValueToSpecialValue<K, A, V>(this Type _enumType, Func<A, K> _funcKey, Func<A, V> _funcValue)
        where A : Attribute
    {
        return _enumType.ESF_SpecialValueToSpecialValue<K, A, V>(
            (ee, ev, ef, a) => { return _funcKey.Invoke(a); }, (ee, ev, ef, a) => { return _funcValue.Invoke(a); });
    }

    /// <summary>
    /// 特殊值到特殊值
    /// </summary>
    /// <typeparam name="K">Key类型</typeparam>
    /// <typeparam name="A">属性类型</typeparam>
    /// <typeparam name="V">Value类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcKey">Key函数</param>
    /// <param name="_funcValue">Value函数</param>
    /// <returns>枚举名称与值</returns>
    public static Dictionary<K, V> ESF_SpecialValueToSpecialValue<K, A, V>(this Type _enumType, 
        Func<object, object, MemberInfo, A, K> _funcKey, Func<object, object, MemberInfo, A, V> _funcValue)
    where A : Attribute
    {
        return ESF_EnumKeyToValue(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return _funcKey.Invoke(eEnum, eValue, eFieldInfo, _enumForAttribute<A>(eFieldInfo)[0]);
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return _funcValue.Invoke(eEnum, eValue, eFieldInfo, _enumForAttribute<A>(eFieldInfo)[0]);
            }
        );
    }
    #endregion

    #endregion

    #region ToX 枚举到X组

    #region ToEnum 枚举组
    /// <summary>
    /// 枚举名称与值
    /// </summary>
    /// <typeparam name="E">枚举类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static List<E> ESF_ToEnum<E>(this Type _enumType)
        where E : Enum
    {
        return ESF_EnumToList(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return eEnum;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return (E)eEnum;
            }
        );
    }
    #endregion

    #region ToValue 枚举值组
    /// <summary>
    /// 枚举值组
    /// </summary>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static List<int> ESF_ToValue(this Type _enumType)
    {
        return _enumType.ESF_ToValue<int>();
    }

    /// <summary>
    /// 枚举值组
    /// </summary>
    /// <typeparam name="V">值类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static List<V> ESF_ToValue<V>(this Type _enumType)
    {
        return ESF_EnumToList(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return eEnum;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return (V)eValue;
            }
        );
    }
    #endregion

    #region ToAttribute 枚举Attribute组
    /// <summary>
    /// 枚举Attribute组
    /// </summary>
    /// <typeparam name="A">值类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static List<A> ESF_ToAttribute<A>(this Type _enumType)
        where A : Attribute
    {
        return ESF_EnumToList(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return eEnum;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return _enumForAttribute<A>(eFieldInfo)[0];
            }
        );
    }
    #endregion

    #region ToSpecialValue 特殊值
    /// <summary>
    /// 枚举特殊值组
    /// </summary>
    /// <typeparam name="A">属性类型</typeparam>
    /// <typeparam name="R">值类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static List<R> ESF_ToESpecialValue<E, R>(this Type _enumType, Func<E, R> _funcResult)
        where E : Enum
    {
        return _enumType.ESF_ToSpecialValue<E, object, Attribute, R>((e, v, a) => { return _funcResult.Invoke(e); });
    }

    /// <summary>
    /// 枚举特殊值组
    /// </summary>
    /// <typeparam name="A">属性类型</typeparam>
    /// <typeparam name="R">值类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static List<R> ESF_ToASpecialValue<A, R>(this Type _enumType, Func<A, R> _funcResult)
        where A : Attribute
    {
        return _enumType.ESF_ToSpecialValue<object, object, A, R>((e, v, a) => { return _funcResult.Invoke(a); });
    }

    /// <summary>
    /// 枚举特殊值组
    /// </summary>
    /// <typeparam name="A">属性类型</typeparam>
    /// <typeparam name="R">值类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static List<R> ESF_ToVSpecialValue<V, R>(this Type _enumType, Func<V, R> _funcResult)
    {
        return _enumType.ESF_ToSpecialValue<object, V, Attribute, R>((e, v, a) => { return _funcResult.Invoke(v); });
    }

    /// <summary>
    /// 枚举特殊值组
    /// </summary>
    /// <typeparam name="A">属性类型</typeparam>
    /// <typeparam name="R">值类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>枚举名称与值</returns>
    public static List<R> ESF_ToSpecialValue<E, V, A, R>(this Type _enumType, Func<E, V, A, R> _funcResult)
        where A : Attribute
    {
        return ESF_EnumToList(_enumType,
            (eEnum, eValue, eFieldInfo) =>
            {
                return eEnum;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return eValue;
            },
            (eEnum, eValue, eFieldInfo) =>
            {
                return _funcResult.Invoke((E)eEnum, (V)eValue, _enumForAttribute<A>(eFieldInfo)[0]);
            }
        );
    }
    #endregion

    #endregion

    #region EnumKeyToValue 枚举转换成指定字典
    /// <summary>
    /// int sizeof偏移量
    /// </summary>
    static readonly int msrInt_sizeof = sizeof(int) * 8;
    /// <summary>
    /// 枚举字典映射
    /// </summary>
    static Dictionary<long, Dictionary<long, Dictionary<long, Dictionary<long, object>>>> msEnumEKVRDictionaryMaping =
        new Dictionary<long, Dictionary<long, Dictionary<long, Dictionary<long, object>>>>();
    /// <summary>
    /// 枚举转换成指定字典
    /// </summary>
    /// <typeparam name="K">Key类型</typeparam>
    /// <typeparam name="V">Value类型</typeparam>
    /// <typeparam name="R">Value返回结果类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcKey">Key值</param>
    /// <param name="_funcValue">Value值</param>
    /// <param name="_funcResult">Result值</param>
    /// <returns>字典</returns>
    public static Dictionary<K, R> ESF_EnumKeyToValue<K, V, R>(Type _enumType,
        Func<object, object, MemberInfo, K> _funcKey,
        Func<object, object, MemberInfo, V> _funcValue,
        Func<object, object, MemberInfo, R> _funcResult)
    {
        ESF_IsEnum(_enumType);

        long funKey = _funcKey.GetHashCode();
        long funcValue = _funcValue.GetHashCode();
        long funcResult = _funcResult.GetHashCode();
        long enumType = _enumType.GetHashCode();
        long gkKey = typeof(K).GetHashCode();
        long gvKey = typeof(V).GetHashCode();
        long grKey = typeof(R).GetHashCode();


        long eKey = (funKey << msrInt_sizeof) + funcValue;
        long kKey = (funcResult << msrInt_sizeof) + enumType;
        long vKey = (gkKey << msrInt_sizeof) + gvKey;
        long rKey = grKey;

       
        #region 构建字典
        Dictionary<long, Dictionary<long, Dictionary<long, object>>> eValue = null;
        if (!msEnumEKVRDictionaryMaping.TryGetValue(eKey, out eValue))
        {
            eValue = new Dictionary<long, Dictionary<long, Dictionary<long, object>>>();
            msEnumEKVRDictionaryMaping[eKey]=eValue;
        }
        Dictionary<long, Dictionary<long, object>> ekValue = null;
        if (!eValue.TryGetValue(kKey, out ekValue))
        {
            ekValue = new Dictionary<long, Dictionary<long, object>>();
            eValue[kKey]=ekValue;
        }
        Dictionary<long, object> ekvValue = null;
        if (!ekValue.TryGetValue(vKey, out ekvValue))
        {
            ekvValue = new Dictionary<long, object>();
            ekValue[vKey] = ekvValue;
        }
        #endregion        
        object result = null;
        bool hasKey = ekvValue.TryGetValue(rKey, out result);
        Dictionary<K, R> dic = result as Dictionary<K, R>;
        if (!hasKey || (dic != null && dic.Count <= 0))
        {
            dic = new Dictionary<K, R>();
            string[] enumNames = Enum.GetNames(_enumType);
            if (enumNames != null && enumNames.Length > 0)
            {
                #region 解析Func
                K tmpK;
                V tmpV;
                R tmpR;
                string name = string.Empty;
                FieldInfo field = null;
                object tEnum = null;
                object tValue = null;
     
                for (int i=0;i< enumNames.Length;i++)
                {                    
                    tValue = Enum.Parse(_enumType, enumNames[i]);
                    name = enumNames[i];                   
                    if (OnEnumCanAddMaping(_enumType, tValue))
                    {                                   
                        tEnum = Enum.Parse(_enumType, name);                        
                        field = _enumNameForFieldInfo(_enumType, name);                        
                        tmpK = _funcKey(tEnum, tValue, field);
                        tmpV = _funcValue(tEnum, tValue, field);
                        tmpR = _funcResult(tEnum, tValue, field);
                        dic[tmpK] = tmpR;
                    }                    
                }               
                #endregion
            }
            ekvValue[rKey] = dic;
            result = dic;
        }
        return (Dictionary<K, R>)result;
    }
    #endregion

    #region EnumToList 枚举转换成指定数组
    /// <summary>
    /// 枚举字典映射
    /// </summary>
    static Dictionary<long, Dictionary<long, Dictionary<long, Dictionary<long, object>>>> msEnumEKVRListMaping =
        new Dictionary<long, Dictionary<long, Dictionary<long, Dictionary<long, object>>>>();
    /// <summary>
    /// 枚举转换成指定字典
    /// </summary>
    /// <typeparam name="K">Key类型</typeparam>
    /// <typeparam name="V">Value类型</typeparam>
    /// <typeparam name="R">Value返回结果类型</typeparam>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_funcKey">Key值</param>
    /// <param name="_funcValue">Value值</param>
    /// <param name="_funcResult">Result值</param>
    /// <returns>字典</returns>
    public static List<R> ESF_EnumToList<K, V, R>(this Type _enumType,
        Func<object, object, MemberInfo, K> _funcKey,
        Func<object, object, MemberInfo, V> _funcValue,
        Func<object, object, MemberInfo, R> _funcResult)
    {
        ESF_IsEnum(_enumType);

        long funKey = _funcKey.GetHashCode();
        long funcValue = _funcValue.GetHashCode();
        long funcResult = _funcResult.GetHashCode();
        long enumType = _enumType.GetHashCode();
        long gkKey = typeof(K).GetHashCode();
        long gvKey = typeof(V).GetHashCode();
        long grKey = typeof(R).GetHashCode();


        long eKey = (funKey << msrInt_sizeof) + funcValue;
        long kKey = (funcResult << msrInt_sizeof) + enumType;
        long vKey = (gkKey << msrInt_sizeof) + gvKey;
        long rKey = grKey;

        #region 构建字典
        Dictionary<long, Dictionary<long, Dictionary<long, object>>> eValue = null;
        if (!msEnumEKVRListMaping.TryGetValue(eKey, out eValue))
        {
            eValue = new Dictionary<long, Dictionary<long, Dictionary<long, object>>>();
            msEnumEKVRListMaping[eKey] = eValue;
        }
        Dictionary<long, Dictionary<long, object>> ekValue = null;
        if (!eValue.TryGetValue(kKey, out ekValue))
        {
            ekValue = new Dictionary<long, Dictionary<long, object>>();
            eValue[kKey] = ekValue;
        }
        Dictionary<long, object> ekvValue = null;
        if (!ekValue.TryGetValue(vKey, out ekvValue))
        {
            ekvValue = new Dictionary<long, object>();
            ekValue[vKey] = ekvValue;
        }
        #endregion

        object result = null;
        bool hasKey = ekvValue.TryGetValue(rKey, out result);
        List<R> list = result as List<R>;
        if (!hasKey || (list != null && list.Count <= 0))
        {
            list = new List<R>();
            string[] enumNames = Enum.GetNames(_enumType);
            if (enumNames != null && enumNames.Length > 0)
            {                
                #region 解析Func
                K tmpK;
                V tmpV;
                R tmpR;
                string name = string.Empty;
                FieldInfo field = null;
                object tEnum = null;
                object tValue = null;
                for (int i = 0; i < enumNames.Length; i++)
                {
                    tValue = Enum.Parse(_enumType, enumNames[i]);
                    if (OnEnumCanAddMaping(_enumType, tValue))
                    {
                        name = enumNames[i];
                        tEnum = Enum.Parse(_enumType, name);
                        field = _enumNameForFieldInfo(_enumType, name);
                        tmpK = _funcKey(tEnum, tValue, field);
                        tmpV = _funcValue(tEnum, tValue, field);
                        tmpR = _funcResult(tEnum, tValue, field);
                        list.Add(tmpR);
                    }
                }
                #endregion
            }
            ekvValue[rKey] = list;
            result = list;
        }
        return (List<R>)result;
    }
    #endregion

    #region OnEnumCanAddMaping 枚举值是否可加入到映射
    /// <summary>
    /// 枚举值是否可加入到映射
    /// </summary>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_enumValue">枚举值</param>
    /// <returns>true:可加入,false:不可加入</returns>
    private static bool OnEnumCanAddMaping(Type _enumType, object _enumValue)
    {
        bool result = true;
        bool isFlags = _enumType.ESF_FirstAttribute<FlagsAttribute>() != null;
        if (isFlags)
        {
            double value = 0;
            TypeCode code = Convert.GetTypeCode(_enumValue);            
            if (code == TypeCode.Object)
            {
                value = (double)_enumValue;
            }
            else
            {
                value = Convert.ToDouble(_enumValue);
            }
            result = value > 0;
        }
        return result;
    }
    #endregion

    #region FirstAttribute 获得第一个指定属性
    /// <summary>
    /// 类型与首个属性映射
    /// Key:FieldInfo
    /// Value:[Key:泛型A:Attribute类型,Value:A:Attribute属性]
    /// </summary>
    static Dictionary<int, Dictionary<int, Attribute>> mMemberInfoForFirstAttributeMaping = new Dictionary<int, Dictionary<int, Attribute>>();
    /// <summary>
    /// 获得第一个指定属性
    /// </summary>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_memberInfo">FieldInfo</param>
    /// <returns>属性</returns>
    public static A ESF_FirstAttribute<A>(this MemberInfo _memberInfo) where A : Attribute
    {
        int tKey = _memberInfo.GetHashCode();
        Dictionary<int, Attribute> attrDic = null;
        if (!mMemberInfoForFirstAttributeMaping.TryGetValue(tKey, out attrDic))
        {
            attrDic = new Dictionary<int, Attribute>();
        }
        int aKey = typeof(A).GetHashCode();
        Attribute attValue = null;
        if (!attrDic.TryGetValue(aKey, out attValue))
        {
            object[] attrs = _memberInfo.GetCustomAttributes(typeof(A), true);
            if (attrs != null && attrs.Length > 0)
            {
                attValue = (A)attrs[0];
            }
            attrDic.Add(aKey, attValue);
        }
        return attValue != null ? (A)attValue : null;
    }
    #endregion    

    #region IsEnum 是否是枚举
    /// <summary>
    /// 是否是枚举
    /// </summary>    
    /// <returns>true:是枚举,false:不是枚举</returns>
    static bool OnIsEnum<E>()
    {
        return typeof(E).ESF_IsEnum();
    }
    /// <summary>
    /// 检测是否是枚举
    /// </summary>
    /// <param name="_type">类型</param>
    /// <returns>true:是枚举,false:不是枚举</returns>
    public static bool ESF_IsEnum(this Type _type)
    {
        bool isEnum = _type.IsEnum;
        if (!_type.IsEnum)
        {
            throw new ArgumentException(string.Format("【{0}】必须是枚举类型！", _type.FullName));
        }
        return isEnum;
    }
    #endregion

    #region ToAlias 获得当前枚举的别名   
    /// <summary>
    /// 获得当前枚举的别名
    /// </summary>
    /// <param name="_enum">枚举</param>
    /// <returns>别名</returns>
    public static string ESF_ToAlias(this Enum _enum)
    {        
        string result = _enum.ToString();
        ESF_AliasTooltipAttribute attr = _enum.ESF_FirstAttribute<ESF_AliasTooltipAttribute>();
        if (attr != null)
        {
            result = attr.alias;
        }
        return result;
    }
    #endregion

    #region ToAlias 获得当前类别的别名   
    /// <summary>
    /// 获得当前类别的别名
    /// </summary>
    /// <param name="_type">类别</param>
    /// <returns>别名</returns>
    public static string ESF_ToAlias(this Type _type)
    {
        string result = _type.Name;
        ESF_AliasTooltipAttribute attr = _type.ESF_FirstAttribute<ESF_AliasTooltipAttribute>();
        if (attr != null)
        {
            result = attr.alias;
        }
        return result;
    }
    #endregion

    /**************内部静态方法*********************************************************************/

    #region _enumNameForFieldInfo 枚举字段信息
    /// <summary>
    /// 类型与字段信息映射
    /// Key:枚举Type
    /// Value:[Key:字段名称HashCode,Value:FieldInfo字段]
    /// </summary>
    static Dictionary<int, Dictionary<int, FieldInfo>> mEnumForFieldInfoMaping = new Dictionary<int, Dictionary<int, FieldInfo>>();
    /// <summary>
    /// 获得枚举的指定名称的FieldInfo
    /// </summary>
    /// <param name="_enumType">枚举类型</param>
    /// <param name="_enumName">枚举名称</param>
    /// <returns>FieldInfo</returns>
    static FieldInfo _enumNameForFieldInfo(Type _enumType, string _enumName)
    {
        Dictionary<int, FieldInfo> dic = _enumNameForFieldInfo(_enumType);
        int key = _enumName.GetHashCode();
        FieldInfo result = null;
        dic.TryGetValue(key, out result);
        return result;
    }

    /// <summary>
    /// 解析枚举字段信息
    /// </summary>
    /// <param name="_enumType">枚举类型</param>
    /// <returns>字段信息[Key:字段名称HashCode,Value:字段信息]</returns>
    static Dictionary<int, FieldInfo> _enumNameForFieldInfo(Type _enumType)
    {
        int tKey = _enumType.GetHashCode();
        Dictionary<int, FieldInfo> result = null;
        if (!mEnumForFieldInfoMaping.TryGetValue(tKey, out result))
        {
            result = new Dictionary<int, FieldInfo>();
            FieldInfo[] fields = _enumType.GetFields();
            if (fields != null)
            {
                for (int i = 0; i < fields.Length; i++)
                {
                    if (!fields[i].IsSpecialName)
                    {
                        result[fields[i].Name.GetHashCode()] = fields[i];
                    }
                }
            }
            mEnumForFieldInfoMaping[tKey] = result;
        }
        return result;
    }
    #endregion

    #region _enumForAttribute 枚举字段属性信息
    /// <summary>
    /// MemberInfo Attribute映射
    /// </summary>
    static Dictionary<int, Dictionary<int, object>> mMemberInfoAttributeMaping = new Dictionary<int, Dictionary<int, object>>();
    /// <summary>
    /// 枚举ForAttribute
    /// </summary>
    /// <typeparam name="A">属性类型</typeparam>
    /// <param name="_memberInfo">字段属性</param>
    /// <returns>属性组</returns>
    static List<A> _enumForAttribute<A>(MemberInfo _memberInfo) where A : Attribute
    {
        object result = null;
        int mKey = _memberInfo.GetHashCode();
        int tKey = typeof(A).GetHashCode();
        Dictionary<int, object> attrMap = null;
        if (!mMemberInfoAttributeMaping.TryGetValue(mKey, out attrMap))
        {
            attrMap = new Dictionary<int, object>();
            mMemberInfoAttributeMaping[mKey] = attrMap;
        }
        if (!attrMap.TryGetValue(tKey, out result))
        {
            List<A> lstA = new List<A>();
            object[] attrs = _memberInfo.GetCustomAttributes(typeof(A), true);
            if (attrs != null && attrs.Length > 0)
            {
                for (int i = 0; i < attrs.Length; i++)
                {
                    lstA.Add((A)attrs[i]);
                }
            }
            if (lstA.Count <= 0)
            {
                lstA.Add(null);
            }
            attrMap[tKey] = lstA;
            result = lstA;
        }
        return (List<A>)result;
    }
    #endregion    
}
